package com.china08.yxyapi.repository.db;

import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.china08.yxyapi.entity.db.res.YxyResSubject;

public interface YxyResSubjectRepository extends JpaRepository<YxyResSubject, String> {
    /**
     * 根据ID修改
     * 
     * @param id
     */
    @Transactional
    void deleteById(String id);

    long countByName(String name);

    YxyResSubject findTopByIdStartingWithOrderByIdDesc(String id);

    /**
     * 
     * @param searchWords
     * @param searchWords2
     * @return
     */
    @Query(value = "SELECT subject from YxyResSubject subject where subject.name Like %?1% OR subject.descript  Like %?2%")
    Page<YxyResSubject> findByNameLikeOrDescriptLike(String searchWords, String searchWords2, Pageable pageable);

    List<YxyResSubject> findByIdInOrderByOrderNum(List<String> listSubjectId);

    YxyResSubject findByName(String name);

    YxyResSubject findById(String subject);

    List<YxyResSubject> findByIdIn(Set<String> subjectIds);

    @Query(value = "SELECT id,NAME FROM yxy_res.yxy_res_subject WHERE id IN(SELECT DISTINCT(SUBJECT) FROM yxy_yxy.yxy_yxy_ques_know WHERE stage=?1 ) AND STATUS=1 ORDER BY order_num", nativeQuery = true)
    List<Object[]> findSubject(String stage);
}
